package com.pesilo.mvc.view.component.module.backgroundModule
{
	import com.greensock.TweenMax;
	import com.pesilo.base.BaseComponent;
	import com.pesilo.event.PesiloEvent;
	import com.pesilo.mvc.model.vo.BackgroundImageVO;
	import com.pesilo.mvc.model.vo.VideoVO;
	import com.pesilo.mvc.view.component.module.commonModule.VideoComponent;
	import com.pesilo.norms.Norms;
	
	import flash.display.Sprite;
	import flash.events.Event;

	public class BackgroundComponent extends BaseComponent
	{
		private var bgVO:BackgroundImageVO;
		private var mc:Sprite;
		private var videoVO:VideoVO;
		private var videoCmp:VideoComponent;
		
		public function BackgroundComponent(bgVO:BackgroundImageVO)
		{
			this.bgVO=bgVO;
			super();
		}
		
		override public function initUI():void{
			mc=new Sprite();
			this.addChild(mc);
			
			switch(bgVO.type){
				case Norms.BG_STATIC:
					createStaticBackground(bgVO);
					break;
				case Norms.BG_TRENDS:
					createTrendsBackground(bgVO)
					break;
				case Norms.BG_VIDEO:
					createVideoBackground(bgVO);
					break;
			}
		}
		
		private function createVideoBackground(obj:Object):void
		{
			var sizeObjec:Object={"url":bgVO.list[0].img,"width": 460, "height": 320};
			videoVO=new VideoVO(sizeObjec);
			videoCmp=new VideoComponent();
			mc.addChild(videoCmp);
		}
		
		private function createTrendsBackground(vo:BackgroundImageVO):void
		{
			while(mc.numChildren)mc.removeChildAt(0);
			var bg:bgComponent=new bgComponent(vo);
			mc.addChild(bg);
		}
		
		private function createStaticBackground(bgVO:BackgroundImageVO):void
		{
			var str:String=bgVO.list[0].img;
			var imgCmp:ImgComponent=new ImgComponent(str);
			mc.addChild(imgCmp);
		}
		
		override public function initEvent(b:Boolean=true):void{
			if(b){
				if(bgVO.type==Norms.BG_VIDEO){
					videoCmp.addEventListener(PesiloEvent.VIDEO_READY_EVENT,__videoReadyHandle);
					videoCmp.addEventListener(PesiloEvent.VIDEO_TIME_COMPLETE_EVENT,__videoTimeCompleteHandle);
				}
			}else{
				if(bgVO.type==Norms.BG_VIDEO){
					
				}
			}
		}
		
		protected function __videoTimeCompleteHandle(event:PesiloEvent):void
		{
			videoCmp.play();
		}
		
		protected function __videoReadyHandle(event:PesiloEvent):void
		{
			TweenMax.delayedCall(1,initLocation);
		}
		
		override public function initLocation():void{
			if(bgVO.type==Norms.BG_VIDEO){
				videoCmp.x=videoCmp.y=0;
				videoCmp.width = stage.stageWidth;
				videoCmp.height = stage.stageHeight;
			}
		}
		
		override public function start():void{
			if(bgVO.type==Norms.BG_VIDEO){
				videoCmp.playVideoByVO(videoVO);
			}
		}
	}
}